package com.wxp.demo03.button;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.wxp.demo03.R;
import com.wxp.demo03.util.DateUtils;

/**
 * 4.2.2按钮长按事件
 */
public class ButtonLongClickActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setTitle("4.2.2按钮长按事件");
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_button_long_click);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.button_long_click), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //设置长按事件
        //注意：单击不会调用该事件，必须长按才调用。
        TextView tv_result = findViewById(R.id.tv_result);
        Button btn_long_click = findViewById(R.id.btn_long_click);
        btn_long_click.setOnLongClickListener(v -> {
            String desc = String.format("%s 您点击了按钮： %s", DateUtils.getNowTime(), ((Button) v).getText());
            tv_result.setText(desc);
            //是否消耗当前事件。true：操作完后父组件不影响，false：操作完后会让父组件也执行该事件。
            return true;
        });

    }
}